/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.tools;

import java.awt.event.ActionEvent;
import java.util.HashSet;
import java.util.Set;

import dgbgui.DGBGUI;
import dgbgui.command.Command;
import dgbgui.command.CommandStack;
import dgbgui.command.ModelDrivenReplaceQueryCommand;
import dgbgui.gui.dialogs.ModelDrivenFindDialog;
import dgbgui.modeldocument.AttributePrototype;
import dgbgui.modeldocument.ModelDocument;
import dgbgui.modeldocument.ModelDrivenWorkspace;
import dgbgui.modeldocument.Query;
import dgbgui.modeldocument.definition.FilteredConceptDefinition;
import dgbgui.modeldocument.primitives.Concept;
import dgbgui.modeldocument.primitives.ConceptType;

public class ModelDrivenFindTool extends DialogedTool  {

    public ModelDrivenFindTool(Concept concept) {
        m_concept = concept;
    }
    
    public void execute() {
        
        m_dialog = new ModelDrivenFindDialog(m_concept,this);
        
        m_dialog.pack();
        DGBGUI.centerDialog(m_dialog);
        m_dialog.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent event) {
        

        if ( event.getSource() == m_dialog.getOkButton() ) {

            // Create the new list of attribute prototypes
            ConceptType type = (ConceptType)ModelDocument.getInstance().getConceptronType(m_concept);
            Set<String> attribNames = ModelDocument.getInstance().getAttributeNames(type);
            Set<AttributePrototype> newAttribProtos = new HashSet<AttributePrototype>(); 
            for ( String name : attribNames ) {
                String filterString = ((ModelDrivenFindDialog)m_dialog).getAttributeFilterString(name);
                if ( !filterString.equals("") ) {
                    AttributePrototype.FilterType filterType =
                        ((ModelDrivenFindDialog)m_dialog).getAttributeFilterType(name);
                    newAttribProtos.add( new AttributePrototype(name,filterType,filterString));
                }
            }
            
            Query oldQuery = ModelDrivenWorkspace.getInstance().getQuery(m_concept);
            
            FilteredConceptDefinition def = new FilteredConceptDefinition(type);
            assert(def != null);
            def.setAttribProtoTypes(newAttribProtos);
            Query newQuery = new Query(def);
            
            // Replace the intermediate query with the new one
            Command replaceCommand = new ModelDrivenReplaceQueryCommand(m_concept,oldQuery,newQuery);
            CommandStack.getInstance().execute(replaceCommand);
            
            
        } 

        // Close the tool
        doClose();

    }

    // ---------- Fields ----------

    private Concept m_concept;
    

}
